/******************************************************************************/
/* uart_def.h                                                                 */
/******************************************************************************/

#ifndef __UART_DEF_H
#define __UART_DEF_H


/****************************** UART constants ********************************/
#define IER_RBR               (0x01)
#define IER_THRE              (0x02)
#define IER_RLS               (0x04)
#define IER_MODEM             (0x08)
#define IER_CTS               (0x80)
#define IER_ABEO              (0x100)
#define IER_ABTO              (0x200)

#define IIR_RLS               (0x03)
#define IIR_RDA               (0x02)
#define IIR_CTI               (0x06)
#define IIR_THRE              (0x01)
#define IIR_MODEM             (0x00)

#define LSR_RDR               (0x01)
#define LSR_OE                (0x02)
#define LSR_PE                (0x04)
#define LSR_FE                (0x08)
#define LSR_BI                (0x10)
#define LSR_THRE              (0x20)
#define LSR_TEMT              (0x40)
#define LSR_RXFE              (0x80)

#define MCR_DTR               (0x01)
#define MCR_RTS               (0x02)
#define MCR_LOOPB             (0x10)
#define MCR_RTSEN             (0x40)
#define MCR_CTSEN             (0x80)

#define MSR_DCTS              (0x01)
#define MSR_DDSR              (0x02)
#define MSR_TRI               (0x04)
#define MSR_DDCD              (0x08)
#define MSR_CTS               (0x10)
#define MSR_DSR               (0x20)
#define MSR_RI                (0x40)
#define MSR_DCD               (0x80)

#define MODEM_STATUS          (U1MSR & 0xF0)
#define DCD_STATUS            (U1MSR & MSR_DCD)
#define CTS_STATUS            (U1MSR & MSR_CTS)
#define DSR_STATUS            (U1MSR & MSR_DSR)
#define RI_STATUS             (U1MSR & MSR_RI)

#define DTR_LOW               (U1MCR_bit.DTR = 0)
#define RTS_LOW               (U1MCR_bit.RTS = 0)
#define DTR_HIGH              (U1MCR_bit.DTR = 1)

#define UART_BUFF_SIZE        (32)

#define UART0_FIFO_TRLEVEL    (4)
#define UART1_FIFO_TRLEVEL    (8)
#define UART2_FIFO_TRLEVEL    (8)

#define UART_WORD_8           (3)
#define UART_WORD_7           (2)
#define UART_WORD_6           (1)
#define UART_WORD_5           (0)

#define UART_STOPB_2          (1)
#define UART_STOPB_1          (0)

#define UART_PARITY_EN        (1)
#define UART_PARITY_DIS       (0)

#define UART_ODD_PARITY       (0)
#define UART_EVEN_PARITY      (1)
#define UART_MARK_PARITY      (2)
#define UART_SPACE_PARITY     (3)


/*************************** Prototypes struct for UART ***********************/
typedef struct
{
    uint8_t wordLen   : 2;
    uint8_t stopBit   : 1;
    uint8_t parityEn  : 1;
    uint8_t paritySel : 2;
    uint8_t dummy     : 2;
} __UART_KEY;


typedef union
UART_LINE_KEY
{
    UART_LINE_KEY() : key(0) {}

    uint8_t key;
    __UART_KEY bitKey;
} __UART_LINE_KEY;


typedef struct
UART_RX_TOOLS
{
    UART_RX_TOOLS() : dataLen(0) {}

    uint8_t buff[UART_BUFF_SIZE];
    uint32_t dataLen;
} __UART_RX_TOOLS;


typedef struct
UART_TX_TOOLS
{
    UART_TX_TOOLS() : pBuff(0), dataLen(0) {}

    const uint8_t * pBuff;
    uint32_t dataLen;
} __UART_TX_TOOLS;


typedef struct
UART_TOOLS
{
    __UART_RX_TOOLS rx;
    __UART_TX_TOOLS tx;
} __UART_TOOLS;



#endif